package com.pay.utils;

import com.alibaba.fastjson.JSONObject;
import com.pay.entity.TbBusinessDetails;
import com.pay.mapper.TbBusinessDetailsMapper;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class MessageUtils {
    @Resource
    TbBusinessDetailsMapper tbBusinessDetailsMapper;

    public String sendMessage(String phone,Integer updateById) throws IOException {
        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";
        String method = "POST";
        String appcode = "e4dc60026946444ca103bbacc31a3431";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        //根据商家id查询当前审核状态
        System.out.println("====================="+updateById+"=========================");
        if(updateById<=0){
            querys.put("param", "审核未通过");
        }
        querys.put("param", "审核通过，请去缴纳会员费");
        querys.put("tpl_id", "TP1711063");
        Map<String, String> bodys = new HashMap<String, String>();


        HttpResponse response=null;
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            //System.out.println(EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String entity = EntityUtils.toString(response.getEntity());
        JSONObject jsonObject = JSONObject.parseObject(entity);
        String returnCode = jsonObject.getString("return_code");
        return returnCode;
    }

}
